package com.evan.review.architecture.mvp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.review.R;
import com.evan.review.architecture.mvp.model.User;

import java.util.List;

/**
 * 用户列表适配器
 * 用于在RecyclerView中显示用户列表
 */
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
    
    private final List<User> users;
    private final Context context;
    private final UserClickListener listener;
    
    public interface UserClickListener {
        void onUserClick(User user);
        void onUserEditClick(User user);
        void onUserDeleteClick(User user);
        void onUserToggleStatusClick(User user);
    }
    
    public UserAdapter(Context context, List<User> users, UserClickListener listener) {
        this.context = context;
        this.users = users;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user_mvp, parent, false);
        return new UserViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
        User user = users.get(position);
        holder.bind(user);
    }
    
    @Override
    public int getItemCount() {
        return users.size();
    }
    
    public void updateData(List<User> newUsers) {
        users.clear();
        users.addAll(newUsers);
        notifyDataSetChanged();
    }
    
    class UserViewHolder extends RecyclerView.ViewHolder {
        private final ImageView ivAvatar;
        private final TextView tvName;
        private final TextView tvEmail;
        private final TextView tvPhone;
        private final TextView tvStatus;
        private final ImageButton btnEdit;
        private final ImageButton btnDelete;
        private final ImageButton btnToggleStatus;
        
        public UserViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_user_avatar);
            tvName = itemView.findViewById(R.id.tv_user_name);
            tvEmail = itemView.findViewById(R.id.tv_user_email);
            tvPhone = itemView.findViewById(R.id.tv_user_phone);
            tvStatus = itemView.findViewById(R.id.tv_user_status);
            btnEdit = itemView.findViewById(R.id.btn_edit_user);
            btnDelete = itemView.findViewById(R.id.btn_delete_user);
            btnToggleStatus = itemView.findViewById(R.id.btn_toggle_status);
        }
        
        public void bind(final User user) {
            // 在实际应用中，这里应该使用Glide或Picasso加载图片
            // Glide.with(context).load(user.getAvatar()).into(ivAvatar);
            
            // 临时设置一个默认头像
            ivAvatar.setImageResource(R.drawable.ic_launcher_foreground);
            
            tvName.setText(user.getName() + " (" + user.getAge() + "岁)");
            tvEmail.setText(user.getEmail());
            tvPhone.setText(user.getPhone());
            
            // 设置状态
            if (user.isActive()) {
                tvStatus.setText("状态: 活跃");
                tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_green_dark));
                btnToggleStatus.setImageResource(android.R.drawable.ic_media_pause);
            } else {
                tvStatus.setText("状态: 禁用");
                tvStatus.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
                btnToggleStatus.setImageResource(android.R.drawable.ic_media_play);
            }
            
            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onUserClick(user);
                }
            });
            
            btnEdit.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onUserEditClick(user);
                }
            });
            
            btnDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onUserDeleteClick(user);
                }
            });
            
            btnToggleStatus.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onUserToggleStatusClick(user);
                }
            });
        }
    }
} 